﻿using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

namespace LitTools
{
    /// <summary>
    /// 区域过滤器
    /// </summary>
    public class AreaListFilter : IRaycastResultFilter
    {
        public List<AreaInfo> areaInfos;
        public RectTransform filterRect;
        public Camera uiCamera;

        public List<RaycastResult> FiltRaycastResults(List<RaycastResult> result)
        {
            List<RaycastResult> targets = new List<RaycastResult>();
            for (int i = 0; i < result.Count; i++)
            {
                for (int j = 0; j < areaInfos.Count; j++)
                {
                    if (areaInfos[j].IsInside(result[i].worldPosition))
                    {
                        targets.Add(result[i]);
                        break;
                    }
                }
            }
            return targets;
        }
    }

    /// <summary>
    /// 区域信息
    /// </summary>
    public class AreaInfo
    {
        private Vector2 position;
        private Vector2 size;

        public AreaInfo(Vector2 position, Vector2 size)
        {
            this.position = position;
            this.size = size;
        }

        /// <summary>
        /// 是否在区域中
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public bool IsInside(Vector2 position)
        {
            float minX = this.position.x - size.x / 2;
            if (position.x < minX) return false;
            float minY = this.position.y - size.y / 2;
            if (position.y < minY) return false;
            float maxX = this.position.x + size.x / 2;
            if (position.x > maxX) return false;
            float maxY = this.position.y + size.y / 2;
            if (position.y > maxY) return false;
            return true;
        }
    }
}